var $ = require('../../common/helper.js');
Page({
	data: {
		data: []
	},
	onLoad: function (options) {
		this.getData();
	},
	getData: function () {
		var _this = this;
		$.get('api.php?app=member&act=person_reduce', function (json) {
			var data = json.data;
			if ($.isArray(data)) {
				for (var i = 0; i < data.length; i++) {
					data[i].selected = false;
					data[i].checkbox = '';
				}
			}
			_this.setData({
				data: data
			});
		});
	},
	select: function (e) {
		var data = this.data.data,
			index = e.currentTarget.dataset.index;
		if (data[index].selected) {
			data[index].selected = false;
			data[index].checkbox = '';
		} else {
			data[index].selected = true;
			data[index].checkbox = 'checkbox-x';
		}
		this.setData({
			data: data
		});
	},
	submit: function () {
		var _this = this,
			data = this.data.data,
			id = [];
		for (var i = 0; i < data.length; i++) {
			if (data[i].selected) {
				id.push(data[i].id);
			}
		}
		if (!id.length) {
			$.overloadError('请选择人员');
			return;
		}
		$.confirm({
			content: '请问您确定要减员吗？',
			success: function () {
				$.post('api.php?app=member&act=person_reduce', { id: id }, function (json) {
					setTimeout(function(){
						$.overloadSuccess('提交成功');
					}, 500);
					_this.getData();
				});
			}
		});
	}
})